#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>

#include <arpa/inet.h>

// argv[1]: ipaddr_str
int main(int argc , char * argv[])
{
    int sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    struct sockaddr_in sockrcv;

    sockrcv.sin_family =AF_INET;
    sockrcv.sin_port=htons(12100);

    inet_pton(AF_INET, argv[1], &sockrcv.sin_addr.s_addr);  // 输入ip

    int ret = connect(sockfd,(const struct sockaddr*)&sockrcv,sizeof(sockrcv));
    if ( 0 != ret)
    {
        perror("connect\n");
        exit(errno);
    }
    else{
        printf("connect OK\n");
    }

    char sendbuf[] = "Hello Nobody";
    int tCount =10;

    int len = sizeof(sendbuf);
    printf("size = %d\n", len);
    while(tCount--)
    {
        int nsend = send(sockfd, sendbuf, len, 0);
        if ( len == nsend)
            printf("send successful\n");
        else
            perror("Send failed\n");
        //sleep();
    }
}